<template>
  <van-tabbar v-model="active" class="tab-bar">
    <van-tabbar-item @click="goHome">
      <span>首页</span>
      <template #icon="props">
        <img :src="props.active ? icon.active : icon.inactive" />
      </template>
    </van-tabbar-item>
    <van-tabbar-item @click="goProduct">
      <span>产品</span>
      <template #icon="props">
        <img :src="props.active ? icon.activec : icon.inactivec" />
      </template>
    </van-tabbar-item>
    <van-tabbar-item @click="goMine">
      <span>我的</span>
      <template #icon="props">
        <img :src="props.active ? icon.activem : icon.inactivem" />
      </template>
    </van-tabbar-item>
  </van-tabbar>
</template>
<script>
export default {
  name: 'TabBar',
  props: {
    defaultPath: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      active: 0,
      icon: {
        active: require('../assets/image/home/homeo.png'),
        inactive: require('../assets/image/home/home.png'),
        activec: require('../assets/image/home/producto.png'),
        inactivec: require('../assets/image/home/product.png'),
        activem: require('../assets/image/home/mineo.png'),
        inactivem: require('../assets/image/home/mine.png'),
      },
    }
  },
  watch: {
    $route: {
      handler(roter){
        this.switchTab(roter.path);
      },
      // 深度观察监听
      deep: true
    }
  },
  created(){
    if(this.defaultPath !== ""){
      this.switchTab(this.defaultPath)
    }
  },
  methods: {
    switchTab(path){
      if(/^\/home/.test(path)){
        this.active = 0;
      }
      if(/^\/product/.test(path)){
        this.active = 1;
      }
      if(/^\/mine/.test(path)){
        this.active = 2;
      }
    },
    handleChange(value) {
      this.$emit('change', value)
    },
    goHome(){
      this.$router.push({path:'/home'})
    },
    goProduct(){
      this.$router.push({
        name: 'Product',
        params: {
          type: 0,
          deploy: 0
        }
      })
    },
    goMine(){
      if(this.$store.state.user){
        this.$router.push({path:'/mine'})
      }else{
        this.$router.push({path:'/login/0'})
      }
    },
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.tab-bar .van-tabbar-item--active{
  color: #585df9;
}
</style>
